'''
Created on Jul 31, 2013
Python 3.3.2
@author: Nick
'''
from tkinter import *



notNotes=['h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'] #holds all the values that should not be entered as notes
class GUI():
    def __init__ (self, master):
        
        
        
        #-----------------------variables--------------------------------------------------#
        self.user_input = [] #holds the output list of user inputs
        self.song_melody_list = [] #holds the ouput list for melody
        self.song_length_tested = 0 #hold the song length input
        self.feedback_message = "" #holds the text that will go into the label for the erros message
        #-----------------------Variables----End--------------------------------------------#
        
        
        #-----------------------frameNick--------------------------------------------------#
        self.frameNick = Frame(master)
        self.frameNick.pack()
        #-----------------------frameNick------ENd-----------------------------------------#
        
        
        
        #------------------------------Title----------------------------------------------#
        self.title = Label(self.frameNick, text="Automated Music Maker")
        self.title.grid(row = 0, column = 0, columnspan = 2)
        #-------------------------------Title--------End----------------------------------#
        
        
        
        #------------------------------Song-Name-------------------------------------------#
        self.song_name_label = Label(self.frameNick, text="Song Name:")
        self.song_name_label.grid(row = 1, column = 0)
        self.song_name_entry = Entry(self.frameNick)
        self.song_name_entry.grid(row = 1, column = 1)
        #------------------------------Song-Name-End---------------------------------------#
        
        
        
        #-------------------Song--------Length-------------------------------------------#
        self.song_length_label = Label(self.frameNick, text="Select Song Length(In number of times the tune repeats):")
        self.song_length_label.grid(row = 2, column = 0)
        self.song_length = Entry(self.frameNick)
        self.song_length.grid(row = 2, column = 1)
        #-------------------Song--------Length--------end---------------------------------#
        
        
        
        #-------------------Song--------melody-------------------------------------------#
        self.song_melody_label = Label(self.frameNick, text="Enter a Melody(Example: a,b16,c,d2,e):")
        self.song_melody_label.grid(row = 3, column = 0)
        self.song_melody = Entry(self.frameNick)
        self.song_melody.grid(row = 3, column = 1)
        #-------------------Song--------melody--------end---------------------------------#
        
        
        
        #-------------------Song--------Volume-------------------------------------------#
        self.song_volume_label = Label(self.frameNick, text="Select Song Volume(0-200):")
        self.song_volume_label.grid(row = 4, column = 0)
        self.song_volume = Scale(self.frameNick, from_=0, to=200, orient=HORIZONTAL)
        self.song_volume.grid(row = 4, column = 1)
        #-------------------Song--------Length--------end---------------------------------#
        
        
        
        #-------------------Song--------Speed-----------------------------------------#
        self.song_speed_label = Label(self.frameNick, text="Select Default Note Type:")
        self.song_speed_label.grid(row = 5, column = 0)
        self.song_speed = StringVar(self.frameNick)
        self.song_speed.set("Quarter Notes") # default value
        self.song_speed_menu = OptionMenu(self.frameNick, self.song_speed, "Full Notes", "Half Notes", "Quarter Notes", "8th Notes", "16th Notes")
        self.song_speed_menu.grid(row = 5, column = 1)
        #-------------------Song--------Speed--------end---------------------------------#
        
        
        
        #-------------------Select--Instrument-----------------------------------------#
        self.song_instrument_label = Label(self.frameNick, text="Select Instrument:")
        self.song_instrument_label.grid(row = 6, column = 0)
        self.song_instrument = StringVar(self.frameNick)
        self.song_instrument.set("piano") # default value
        self.song_instrument_menu = OptionMenu(self.frameNick, self.song_instrument, "piano", "harpsichord","glock","vibes"
                                               ,"marimba","organ","guitar","bass"
                                               ,"violin","cello","harp","timps"
                                               ,"voice","trumpet","tuba","horn"
                                               ,"alto sax","oboe","bassoon","clarinet"
                                               ,"flute","recorder","bottle","whistle"
                                               ,"fifths","halo","goblins","koto"
                                               ,"bagpipe","taiko","toms","breath"
                                               ,"seasshore","bird","phone","applause")
        self.song_instrument_menu.grid(row = 6, column = 1)
        #-------------------Select--Instrument--End-------------------------------------#
        
        
        
        #------------------------------feedback---------------------------------------------#
        self.feedback = StringVar()
        self.feedback_label = Label(self.frameNick, textvariable=self.feedback)
        self.feedback_label.grid(row = 7, column = 0)
        #------------------------------feedback---end------------------------------------#
        
        
        
        #---------------------Done---------------------------------------------------------#
        self.done_button = Button(self.frameNick, text="Done", command=self.make_song)
        self.done_button.grid(row = 8, column = 1)
        #----------------------Done-----End-------------------------------------------------#
        
        
        
    def make_song(self):
        self.user_input = [] #starts with a fresh list(for use if the first try of the gui was input incorrectly)
        if self.song_name_entry.get() == "":
            self.user_input.append("Automated Music Maker Song") #if nothing was input sets a defualt value
        else:
            self.user_input.append(self.song_name_entry.get()) #if something was unput sets it as the input
        
        
        try: #try to turn the string into an int
            if self.song_length.get() != "": #check to see if ther is an input
                self.song_length_tested = int(self.song_length.get()) #if there is trys to turn it into an int
            self.feedback_message = "Loading..." #if it works the feedback message = loading...
        except ValueError: #if it cannot turn it into a string
            self.feedback_message = "Inputs Failed!! Please enter different values and try again" #sets a error message as feedback
            print("error") #prints error
        if self.song_length_tested == 0: #if the input is = to 0 zero then it sets it to one so something is actually written to the file
            self.user_input.append(1) #adds 1 to the list
        else:
            self.user_input.append(self.song_length_tested) #if it is not equal to 0 adds what ever value was input
        
        
        if self.song_volume.get() == 0: #check to see if the volume is equal t 0
            self.user_input.append(100) #if it is sets its default equal to 100
        else:
            self.user_input.append(self.song_volume.get()) #if not equal to 0 sets it as whatever the input was
        
        
        if self.song_speed.get() == "Full Notes" : #checks to see if full notes were selected
            self.user_input.append(1) #if they were sets the note type = to 1
        elif self.song_speed.get() == "Half Notes" : #checks to see if half notes were selected
            self.user_input.append(1/2) #if they were sets note type = to 0.5
        elif self.song_speed.get() == "Quarter Notes" : #checks to see if quarter notes were selected
            self.user_input.append(1/4) #if they were sets note type = to 0.25
        elif self.song_speed.get() == "8th Notes" : #checks to see if 8th notes were selected
            self.user_input.append(1/8) #if they were sets note type = to 1/8
        elif self.song_speed.get() == "16th Notes" : #checks to see if 16th notes were selected
            self.user_input.append(1/16) #if they were sets note type = to 1/16
        
        
        self.user_input.append(self.song_instrument.get()) #adds the selected instrument type to the list
        
        if self.song_melody.get() == "": #checks to see if a melody was input
            self.song_melody_list = ['d', 'd16', 'e', 'd', 'g', 'f2', 'd', 'd16', 'e', 'd', 'a', 'g2', 'd', 'd16', 'd', 'b', 'g', 'f', 'e2', 'c', 'c16', 'b', 'g', 'a', 'g2'] #if not sets default happy birthday melody
        else:
            self.song_melody_list = self.song_melody.get().lower().split(",") #sets all of the input to lower case then splits it by the commas
            print(self.song_melody_list) #prints the melody list
            for i in range(len(self.song_melody_list)): #goes through all the inputed notes
                if self.song_melody_list[i] in notNotes: #makes sure the notes are not in the notnotes list
                    self.feedback_message = "Inputs Failed!! Please enter different values and try again" #if they erros sends an error
            
            
        self.send_feedback(self.feedback_message) #sets the feedback message
        print("[[Song Name, Duration, Volume, Speed of Song, Instrument],[Melody]") #prints out a key of how the output is set up fro use in other programs
        print([self.user_input,self.song_melody_list]) #prints out the input list and melody list in one list
        return [self.user_input,self.song_melody_list] #actually returns the list that was just printed

    
    def send_feedback(self, text_in): #this sets the feedback mesage
        self.feedback.set(text_in)
        if text_in != "Loading...": #checks to see if the there is an error
            #self.user_input[0] = None
            pass #if there is an error it passes
        else:
            self.frameNick.quit() #if it has no errors 
            